package devicedbgui;

import devicedbgui.data.DeviceStats;
import devicedbgui.data.TestCaseDAO;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

/** Displays statistics pertaining to device */
public class DeviceStatsPanel extends javax.swing.JPanel {
    Hashtable<Integer, DeviceStats> htDeviceStats = new Hashtable<Integer, DeviceStats>();
     GUI gui;
     String deviceRecord;
     /** Constructor creates all components belonging to panel     */
    public DeviceStatsPanel(GUI gui, String deviceRecord) {
        initComponents();
        this.gui = gui;
        this.deviceRecord = deviceRecord;
        loadData(gui, deviceRecord);
    }
     /** Constructor creates all components belonging to panel     */
    public DeviceStatsPanel(GUI gui) {
        initComponents();
        this.gui = gui;        
        loadData(gui);
    }
    /** Displays error messages
     * @param parent
     * @param error 
     */
    public void showError(JFrame parent, String error)
    {
        JOptionPane.showMessageDialog(parent, error);
    }
    /** Displays test case data from database */
    private void loadData(GUI gui, String deviceRecord)
    {
        TestCaseDAO dd = new TestCaseDAO(gui.connection);
        try {
            ArrayList<DeviceStats> records = dd.getDeviceStats(deviceRecord);
            DefaultTableModel model = (DefaultTableModel)tblDeviceStats.getModel();
            model.getDataVector().clear();
            int index = 0;   
            htDeviceStats.clear();
            for (DeviceStats item : records){ 
                htDeviceStats.put(new Integer(index++), item);
                model.addRow(new Object[]{item.testCase, item.numberOfFailed, item.numberOfPassed});
            }
             
        }
        catch (Exception ex){
            showError(gui.frame, ex.toString());
        }
    }
    /** Displays test case data from database */
    private void loadData(GUI gui)
    {
        TestCaseDAO dd = new TestCaseDAO(gui.connection);
        try {
            ArrayList<DeviceStats> records = dd.getDeviceStats();
            DefaultTableModel model = (DefaultTableModel)tblDeviceStats.getModel();
            model.getDataVector().clear();
            int index = 0;   
            htDeviceStats.clear();
            for (DeviceStats item : records){ 
                htDeviceStats.put(new Integer(index++), item);
                model.addRow(new Object[]{item.testCase, item.numberOfFailed, item.numberOfPassed});
            }
            
        }
        catch (Exception ex){
            showError(gui.frame, ex.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblDeviceStats = new javax.swing.JTable(){    public Component prepareRenderer(TableCellRenderer renderer,
            int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if (rowIndex % 2 == 0 && !tblDeviceStats.isRowSelected(rowIndex)) {
                c.setBackground(new Color(237, 242, 251));
            }
            else if (tblDeviceStats.isRowSelected(rowIndex)){c.setBackground(new Color(184, 207, 229));}
            else { c.setBackground(getBackground());}
            return c;
        }
    }
    ;
    btnTestCaseDetails = new javax.swing.JButton();

    tblDeviceStats.setAutoCreateRowSorter(true);
    tblDeviceStats.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null}
        },
        new String [] {
            "Test Case", "Number of Times TestCase Failed", "Number of times TestCase Passed"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    tblDeviceStats.setColumnSelectionAllowed(true);
    tblDeviceStats.setGridColor(new java.awt.Color(204, 204, 255));
    tblDeviceStats.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane1.setViewportView(tblDeviceStats);
    tblDeviceStats.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

    btnTestCaseDetails.setText("Test Case Details");
    btnTestCaseDetails.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnTestCaseDetailsActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(btnTestCaseDetails, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(btnTestCaseDetails)))
            .addContainerGap())
    );
    }// </editor-fold>//GEN-END:initComponents
/** Displays further information pertaining to selected test case*/
private void btnTestCaseDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTestCaseDetailsActionPerformed
  int[] selected = tblDeviceStats.getSelectedRows();  
    if (selected.length == 0){
        JOptionPane.showMessageDialog(gui.frame, "No item has been selected, please select an item");
        return;
    }
    
    DeviceStats item = htDeviceStats.get(new Integer(selected[0]));
    
    if (item == null)
    {
        JOptionPane.showMessageDialog(gui.frame, "No item has been selected, please select an item");
        return;
    }
    
    // show TestCaseDetails dialog
    TestCaseDetailsPanel p = new TestCaseDetailsPanel(gui,item.testCase);
     gui.tab.add(p);
     gui.tab.setSelectedComponent(p);
     gui.tab.setTabComponentAt( gui.tab.getSelectedIndex(), gui.TabSet("Test Case Details") );
   
}//GEN-LAST:event_btnTestCaseDetailsActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTestCaseDetails;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblDeviceStats;
    // End of variables declaration//GEN-END:variables

}
